package com.dhcc.sdc.cdss.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.sdc.base.dao.SdcCdssContraindicationMatchServiceMapper;
import com.dhcc.sdc.base.entity.SdcCdssContraindicationMatchEntity;
import com.dhcc.sdc.base.entity.ContraindicationEntity;
import com.dhcc.sdc.base.service.IContraindicationService;
import com.dhcc.sdc.cdss.dao.SdcCdssItemMapper;
import com.dhcc.sdc.cdss.entity.SdcCdssItemEntity;
import com.dhcc.sdc.cdss.service.ISdcCdssContraindicationMatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * CDSS推荐方案项目与诊疗方案禁忌症表数据服务层
 * 
 * @ClassName: SdcCdssContraindicationMatchServiceImpl
 * @Description: TODO
 * @author yanghangwei
 * @date 2024-01-29 10:23:49
 */
@Service("cdssContraindicationMatchService")
public class SdcCdssContraindicationMatchServiceImpl extends BaseServiceImpl<SdcCdssContraindicationMatchServiceMapper, SdcCdssContraindicationMatchEntity> implements ISdcCdssContraindicationMatchService {

	@Resource
	private SdcCdssItemMapper sdcCdssItemMapper;
	@Autowired
	private IContraindicationService contraindicationService;

	/**
	 * 根据compare条件查询禁忌症对照数据
	 * @author: YangHangwei
	 * @date:2024-01-29 12:30:01
	 * @param params
	 * @return: Page<ContraindicationEntity> 查询结果
	 */
	@Override
	public Page<ContraindicationEntity> pageMatchContraindication(Map<String, Object> params) {
		List<ContraindicationEntity> contraindicationList = CollUtil.newArrayList();
		String id = (String) params.get("id");
		String compare = (String) params.get("compare");
		String currentPage = (String) params.get("page");
		String size = (String) params.get("rows");
		SdcCdssItemEntity sdcCdssItemEntity = sdcCdssItemMapper.selectById(Long.parseLong(id));
		if (StringUtils.isEmpty(sdcCdssItemEntity)){
			return null;
		}
		params.put("cdssId", sdcCdssItemEntity.getId());
		Page<ContraindicationEntity> resPage = new Page<>(Integer.parseInt(currentPage), Integer.parseInt(size));
		switch (compare){
			case "all":
				contraindicationList = contraindicationService.page(resPage,new HashMap<>());
				List<SdcCdssContraindicationMatchEntity> checkList = this.baseMapper.list(params);
				for (ContraindicationEntity contraindicationEntity : contraindicationList) {
					for (SdcCdssContraindicationMatchEntity sdcCdssContraindicationMatchEntity : checkList) {
						if (Objects.equals(contraindicationEntity.getId(), sdcCdssContraindicationMatchEntity.getContraindicationId())){
							contraindicationEntity.setChecked(1);
						}
					}
				}
				break;
			case "search":
				contraindicationList = contraindicationService.query(resPage,params);
				checkList = this.baseMapper.list(params);
				for (ContraindicationEntity contraindicationEntity : contraindicationList) {
					for (SdcCdssContraindicationMatchEntity sdcCdssContraindicationMatchEntity : checkList) {
						if (Objects.equals(contraindicationEntity.getId(), sdcCdssContraindicationMatchEntity.getContraindicationId())){
							contraindicationEntity.setChecked(1);
						}
					}
				}
				break;
			case "match":
				Page<SdcCdssContraindicationMatchEntity> page = new Page<>(Integer.parseInt(currentPage), Integer.parseInt(size));
				checkList = this.baseMapper.page(page, params);
				if (null != checkList && checkList.size() > 0) {
					for (SdcCdssContraindicationMatchEntity cCEntity : checkList) {
						ContraindicationEntity contraindicationEntity = contraindicationService.selectById(cCEntity.getContraindicationId());
						contraindicationEntity.setChecked(1);
						contraindicationList.add(contraindicationEntity);
					}
				}
				BeanUtil.copyProperties(page,resPage);
				break;
			case "noMatch":
				checkList = this.baseMapper.list(params);
				List<Long> matchIds = new ArrayList<>();
				for (SdcCdssContraindicationMatchEntity cCEntity : checkList) {
					matchIds.add(cCEntity.getContraindicationId());
				}
				contraindicationList = contraindicationService.pageExcludeMatch(resPage,new HashMap<>(),matchIds);
				break;
			default:
				break;
		}
		resPage.setRecords(contraindicationList);
		return resPage;
	}

	/**
	 * 勾选添加cdss和禁忌症关联
	 * @author yanghangwei
	 * @date:2024-01-29 13:30:01
	 * @param params
	 * @return
	 */
	@Override
	public void compare(Map<String, Object> params) {
		String[] contraindicationIdList = params.get("contraindicationIDStr").toString().split(",");
		Long cdssId = (Long.parseLong(params.get("cdssId").toString()));
		SdcCdssItemEntity sdcCdssItemEntity = sdcCdssItemMapper.selectById(cdssId);
		for (String contraindicationId : contraindicationIdList) {
			Wrapper<SdcCdssContraindicationMatchEntity> wrapper = new EntityWrapper<>();
			wrapper.eq("cdss_id",cdssId).eq("contraindication_id",contraindicationId);
			SdcCdssContraindicationMatchEntity sdcCdssContraindicationMatchEntity = this.selectOne(wrapper);
			if (ObjectUtils.isEmpty(sdcCdssContraindicationMatchEntity)){
				sdcCdssContraindicationMatchEntity = new SdcCdssContraindicationMatchEntity();
				sdcCdssContraindicationMatchEntity.setCdssId(cdssId);
				sdcCdssContraindicationMatchEntity.setContraindicationId(Long.valueOf(contraindicationId));
				sdcCdssContraindicationMatchEntity.setEntityId(sdcCdssItemEntity.getQcentityId());
				sdcCdssContraindicationMatchEntity.insert();
			}
		}
	}

	/**
	 * 取消勾选移除cdss和禁忌症关联
	 * @author yanghangwei
	 * @date:2024-01-29 14:30:01
	 * @param params
	 * @return
	 */
	@Override
	public void undo(Map<String, Object> params) {
		String[] contraindicationIdList = params.get("contraindicationIDStr").toString().split(",");
		Long cdssId = (Long.parseLong(params.get("cdssId").toString()));
		for (String contraindicationId : contraindicationIdList) {
			Wrapper<SdcCdssContraindicationMatchEntity> wrapper = new EntityWrapper<>();
			wrapper.eq("cdss_id",cdssId).eq("contraindication_id",contraindicationId);
			this.delete(wrapper);
		}

	}
}